#include <iostream>
#include <event2/event.h>
#include <event2/listener.h>
#ifndef _WIN32
#include <signal.h>
#endif
#include <string.h>
#include "XThreadPool.h"
#include "XTask.h"
#include "XFtpServerCMD.h"
using namespace std;

void listen_cb(struct evconnlistener *ev, evutil_socket_t lfd, 
                                struct sockaddr *sin, int socklen, void *arg){
        cout<<"listen_cb"<<endl;
        XTask *task = new XFtpServerCMD();
        task->sock = lfd;
        XThreadPool::Get()->Dispatch(task);
}

int main(int argc, char const *argv[])
{
#ifdef _WIN32
        //初始化socket库
        WSADATA wsa;
        WSAStartup(MAKEWORD(2,2), &wsa);
#else
        //忽略管道信号 发送数据给已关闭的socket
        if(signal(SIGPIPE,SIG_IGN) == SIG_IGN){
                return 1;
        }
#endif  
        //1. 初始化线程池
        XThreadPool::Get()->Init(10);

        cout<<"test thread pool !"<<endl;

        event_base *base = event_base_new();

        sockaddr_in sin;
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_port = htons(8080);

        evconnlistener *ev = evconnlistener_new_bind(base, listen_cb, base, 
                LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1, 
                (sockaddr *)&sin, sizeof(sin));

        event_base_dispatch(base);

        if(ev)
                evconnlistener_free(ev);

        if(base)
                event_base_free(base);

        cout<<"ftp server"<<endl;
        return 0;
}
